package com.visikard.request;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.visikard.application.APIURL;
import com.visikard.application.VKregisterProperties;
import com.visikard.item.CreateCouponParams;
import com.visikard.net.VkRequestMethod;
import com.visikard.net.VkRestClient;
import com.visikard.utility.HttpsUtils;

public class CouponRequestManage extends BaseRequest {

	public String createCouponRequest(String imageFile, String fkUser, String idUsersMerchant, CreateCouponParams params) {

		String result = null;
		HttpClient httpclient = HttpsUtils.getInstance().sslClient(new DefaultHttpClient());
		// String httpurl = "http://192.168.2.92:8080/ARMAdmin/api/vk/" + APIURL.CREATE_COUPON_;
		String httpurl = VKregisterProperties.VISIKARD_API_CONTEXT_PATH_HTTP + APIURL.CREATE_COUPON_;
		try {

			HttpPost httppost = new HttpPost(httpurl);

			MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			multipartEntity.addPart("fkUser", new StringBody(fkUser));
			multipartEntity.addPart("idUsersMerchant", new StringBody(idUsersMerchant));
			multipartEntity.addPart("couponName", new StringBody(params.getCpName()));
			multipartEntity.addPart("description", new StringBody(params.getCpDescription()));
			multipartEntity.addPart("sDate", new StringBody(params.getCpStartDate()));
			multipartEntity.addPart("eDate", new StringBody(params.getCpEndDate()));
			multipartEntity.addPart("expireDate", new StringBody(params.getCpExpireDate()));
			multipartEntity.addPart("timezone", new StringBody(params.getCpTimeZone()));
			multipartEntity.addPart("enabled", new StringBody(params.getCpEnable() + ""));
			multipartEntity.addPart("percentOff", new StringBody(params.getCpPercentOff() + ""));
			multipartEntity.addPart("maxOff", new StringBody(params.getCpMaxOff() + ""));
			multipartEntity.addPart("listLocationCoupon", new StringBody(params.getCpLocation()));
			multipartEntity.addPart("imageFile", new FileBody(new File(imageFile)));
			httppost.setEntity(multipartEntity);
			HttpResponse response = httpclient.execute(httppost);
			result = EntityUtils.toString(response.getEntity()).trim();

		}
		catch (Exception e) {
			return null;
		}
		return result;
	}

	public String getListCouponRequest(String fkUser, String timezone, String pageIndex, String pageSize) {

		// String httpurl = "http://192.168.2.92:8080/ARMAdmin/api/vk/" + APIURL.GET_LIST_COUPON_;
		String httpurl = VKregisterProperties.VISIKARD_API_CONTEXT_PATH_HTTP + APIURL.GET_LIST_COUPON_;
		VkRestClient rc = new VkRestClient(httpurl);

		rc.addParam("userId", fkUser);
		rc.addParam("timezone", timezone);
		rc.addParam("pageIndex", pageIndex);
		rc.addParam("pageSize", pageSize);
		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	public String deleteCouponRequest(String couponId) {

		// String httpurl = "http://192.168.2.92:8080/ARMAdmin/api/vk/" + APIURL.GET_LIST_COUPON_;
		String httpurl = VKregisterProperties.VISIKARD_API_CONTEXT_PATH_HTTP + APIURL.DELETE_COUPON_;
		VkRestClient rc = new VkRestClient(httpurl);

		rc.addParam("idCoupon", couponId);

		rc.execute(VkRequestMethod.POST);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}
}